package com.sitechasia.webx.components.xtable.cell;

import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.cell.HeaderCell;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelUtils;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.ColumnBuilder;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.util.HtmlBuilder;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：生成列表HeaderCell
*
* @author kuanglingxuan
* Created on 2007-8-2
*/
public class MyHeaderCell implements Cell
{

    public MyHeaderCell()
    {
    }

    public String getExportDisplay(TableModel model, Column column)
    {
        return column.getTitle();
    }

    public String getHtmlDisplay(TableModel model, Column column)
    {
        HtmlBuilder html = new HtmlBuilder();
        String headerClass = null;
        String sortImage = null;
        String sortOrder = null;
        headerClass = column.getHeaderClass();
        if(TableModelUtils.isSorted(model, column.getAlias()))
        {
            sortOrder = model.getLimit().getSort().getSortOrder();
            if(sortOrder.equals("default"))
                sortOrder = "asc";
            else
            if(sortOrder.equals("asc"))
            {
//                headerClass = model.getPreferences().getPreference("table.headerSortClass");
                sortImage = "images/ico_table_up.gif";//BuilderUtils.getImage(model, "sortAsc");
//                System.out.println("sortImage--desc-->"+sortImage);
                sortOrder = "desc";
            } else
            if(sortOrder.equals("desc"))
            {
//                headerClass = model.getPreferences().getPreference("table.headerSortClass");
                sortImage = "images/ico_table_down.gif";//BuilderUtils.getImage(model, "sortDesc");
//                System.out.println("sortImage--default-->"+sortImage);
                sortOrder = "default";
            }
        } else
        {
            sortOrder = "asc";
        }
        buildHeaderHtml(html, model, column, headerClass, sortImage, sortOrder);
        return html.toString();
    }


    protected void buildHeaderHtml(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder)
    {
        html.td(2);
//        if(StringUtils.isNotEmpty(headerClass))
//            html.styleClass(headerClass);
        if(StringUtils.isNotEmpty(column.getHeaderStyle()))
            html.style(column.getHeaderStyle());
        if(StringUtils.isNotEmpty(column.getWidth()))
            html.width(column.getWidth());
        if(column.isSortable())
        {
//        	System.out.println("sortOrder--->"+sortOrder);

        		//生成js方法headerOnmouseover、headerOnmouseout
            	String onmouseover =  (String)column.getAttribute("headerOnmouseover");
            	String onmouseout =  (String)column.getAttribute("headerOnmouseout");
                if(StringUtils.isNotEmpty(onmouseover)){

                    html.onmouseover(onmouseover);
                }else{

                	html.onmouseover("this.style.cursor='pointer'");
                }
                if(StringUtils.isNotEmpty(onmouseout)){
                    html.onmouseout(onmouseout);

                }else{

                	html.onmouseout("this.style.cursor='default'");
                }



            html.onclick((new TableActions(model)).getSortAction(column, sortOrder));
            boolean showTooltips = model.getTableHandler().getTable().isShowTooltips();
            if(showTooltips)
            {
                String headercellTooltip = model.getMessages().getMessage("column.headercell.tooltip.sort");
                html.title(headercellTooltip + " " + column.getTitle());
            }
        }
        html.close();
        if(StringUtils.isNotEmpty(headerClass))
        	html.append("<span class=\""+ column.getHeaderClass()+"\">");
        html.append(column.getTitle());

        if(column.isSortable() && StringUtils.isNotEmpty(sortImage))
        {
            html.nbsp();
            html.img();
            html.src(sortImage);
//            html.style("border:0");
//            html.alt("Arrow");
            html.xclose();
        }
        if(StringUtils.isNotEmpty(headerClass))
        	html.append("</span>");
        html.tdEnd();
    }
}
